import types
import urllib.request
import json
import os

###写入文件
def jsonFile(file,fileData):
    
    file = open(file,'w')
    file.write(fileData)
    file.close()

def get_url(url):
    try:
        #print(url)
        req = urllib.request.Request(url)
        b = urllib.request.urlopen(req)
        data = b.read()
        #print(data)
        return data
    except Exception as e:
        print(e)

def registerUrl(mapi,idx,word):
    url = 'http://hot.crawler.ckcest.cn/hot_words/%s?index=%s&topic=%s&is_foreign=false' % (mapi,idx,word)
    return get_url(url)
    
def get_api_data_all(mapi, path,word):
    if os.path.exists(path) == False:
        os.mkdir(path)
    filepath = path+'/'+mapi+'.json'
    if os.path.exists(filepath):
        os.remove(filepath)
    sjson = '{"index":{'
    i=0
    while i < 30:
        data = registerUrl(mapi,i,word)
        sjson = sjson + '"'+ str(i) +'":' + str(data, encoding='utf-8')
        if i<29:
            sjson = sjson +','
        i+=1

    sjson = sjson +'}}'
    jsonFile(filepath,sjson)

def get_api_data(mapi, path,word):
    if os.path.exists(path) == False:
        os.mkdir(path)
    i=0
    while i < 30:
        filepath = path+'/'+mapi+'_'+str(i)+'.json'
        if os.path.exists(filepath):
            os.remove(filepath) 
        data = registerUrl(mapi,i,word)
        sjson =  str(data, encoding='utf-8')
        jsonFile(filepath,sjson) 
        i+=1
    pass
   

def get_timeline_nodes_url():
    url = 'http://hot.crawler.ckcest.cn/hot_words/get_timeline_nodes'
    return get_url(url)

def get_timeline_nodes(path):
    filepath = path + "get_timeline_nodes.json"
    if os.path.exists(filepath):
        os.remove(filepath)
    data = get_timeline_nodes_url()
    sjson =  str(data, encoding='utf-8')
    jsonFile(filepath,sjson) 
    pass
    
get_timeline_nodes('')

#人工智能
get_api_data('get_timeline_data','ai','%E4%BA%BA%E5%B7%A5%E6%99%BA%E8%83%BD')
#环境
get_api_data('get_timeline_data','enviroment','%E7%8E%AF%E5%A2%83')
#医药
get_api_data('get_timeline_data','medical','%E5%8C%BB%E8%8D%AF')
#能源
get_api_data('get_timeline_data','resource','%E8%83%BD%E6%BA%90')
#智能制造
get_api_data('get_timeline_data','im','%E6%99%BA%E8%83%BD%E5%88%B6%E9%80%A0')
#一带一路
get_api_data('get_timeline_data','obor','%E4%B8%80%E5%B8%A6%E4%B8%80%E8%B7%AF')
#信息技术
get_api_data('get_timeline_data','it','%E4%BF%A1%E6%81%AF%E6%8A%80%E6%9C%AF')
#科技前沿
get_api_data('get_timeline_data','sb','%E7%A7%91%E6%8A%80%E5%89%8D%E6%B2%BF')



#人工智能
get_api_data('get_timeline_news','ai','%E4%BA%BA%E5%B7%A5%E6%99%BA%E8%83%BD')
#环境
get_api_data('get_timeline_news','enviroment','%E7%8E%AF%E5%A2%83')
#医药
get_api_data('get_timeline_news','medical','%E5%8C%BB%E8%8D%AF')
#能源
get_api_data('get_timeline_news','resource','%E8%83%BD%E6%BA%90')
#智能制造
get_api_data('get_timeline_news','im','%E6%99%BA%E8%83%BD%E5%88%B6%E9%80%A0')
#一带一路
get_api_data('get_timeline_news','obor','%E4%B8%80%E5%B8%A6%E4%B8%80%E8%B7%AF')
#信息技术
get_api_data('get_timeline_news','it','%E4%BF%A1%E6%81%AF%E6%8A%80%E6%9C%AF')
#科技前沿
get_api_data('get_timeline_news','sb','%E7%A7%91%E6%8A%80%E5%89%8D%E6%B2%BF')


#人工智能
get_api_data('get_timeline_related_article','ai','%E4%BA%BA%E5%B7%A5%E6%99%BA%E8%83%BD')
#环境
get_api_data('get_timeline_related_article','enviroment','%E7%8E%AF%E5%A2%83')
#医药
get_api_data('get_timeline_related_article','medical','%E5%8C%BB%E8%8D%AF')
#能源
get_api_data('get_timeline_related_article','resource','%E8%83%BD%E6%BA%90')
#智能制造
get_api_data('get_timeline_related_article','im','%E6%99%BA%E8%83%BD%E5%88%B6%E9%80%A0')
#一带一路
get_api_data('get_timeline_related_article','obor','%E4%B8%80%E5%B8%A6%E4%B8%80%E8%B7%AF')
#信息技术
get_api_data('get_timeline_related_article','it','%E4%BF%A1%E6%81%AF%E6%8A%80%E6%9C%AF')
#科技前沿
get_api_data('get_timeline_related_article','sb','%E7%A7%91%E6%8A%80%E5%89%8D%E6%B2%BF')

#人工智能
get_api_data('get_related_patent_expert','ai','%E4%BA%BA%E5%B7%A5%E6%99%BA%E8%83%BD')
#环境
get_api_data('get_related_patent_expert','enviroment','%E7%8E%AF%E5%A2%83')
#医药
get_api_data('get_related_patent_expert','medical','%E5%8C%BB%E8%8D%AF')
#能源
get_api_data('get_related_patent_expert','resource','%E8%83%BD%E6%BA%90')
#智能制造
get_api_data('get_related_patent_expert','im','%E6%99%BA%E8%83%BD%E5%88%B6%E9%80%A0')
#一带一路
get_api_data('get_related_patent_expert','obor','%E4%B8%80%E5%B8%A6%E4%B8%80%E8%B7%AF')
#信息技术
get_api_data('get_related_patent_expert','it','%E4%BF%A1%E6%81%AF%E6%8A%80%E6%9C%AF')
#科技前沿
get_api_data('get_related_patent_expert','sb','%E7%A7%91%E6%8A%80%E5%89%8D%E6%B2%BF')













